package com.example.yucheng.controller;

import com.example.yucheng.domain.Warn;
import com.example.yucheng.service.WarnService;
import com.example.yucheng.util.PageQuery;
import com.example.yucheng.util.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/warn")
public class WarnController {

    @Autowired
    WarnService warnService;

    @GetMapping("/getWarnList")
    public Result getWarnByLimit(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam Integer sort)  {
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageSize(pageSize);
        pageQuery.setPageNum((pageNum-1)*pageSize);
        pageQuery.setSort(sort);
        List<Warn> warnByLimit = warnService.getWarnByLimit(pageQuery);
        int total = warnService.getAllList().size();
        Map re = new HashMap();
        re.put("warnList",warnByLimit);
        re.put("total",total);
        return Result.success(re);
    };
    @PutMapping("/addWarn")
    public Result insertWarn(@RequestBody Warn warn){
        System.out.println(warn.toString());
        int i=0;
        try{
          i = warnService.insertWarn(warn);
        }catch (Exception e){
            return Result.success("添加失败,已存在");
        }

        if (i==1){
            return Result.success("添加成功");
        }else{
            return Result.success("添加失败,已存在");
        }
    }

    @PutMapping("/updateWarn")
    public Result updateWarn(@RequestBody Warn warn){
        int i = warnService.updateWarn(warn);
        if (i==1){
            return Result.success("更新成功");
        }else{
            return Result.success("更新失败");
        }
    }

    @DeleteMapping("/deleteWarn")
    public Result deletewarn(@RequestParam int[] ids){
        for(int temp:ids){
            int i = warnService.deleteWarnById(temp);
        }
        return Result.success("删除成功");
    }

   @GetMapping("/getIsWarnList")
    public Result getIsWarnList(){

       List<Warn> isWarnByLimit = warnService.getIsWarnByLimit();
       return Result.success(isWarnByLimit);
   }


}
